package com.ubs.training.server.dao.impl.hbm;

import com.ubs.training.server.dao.TypeDao;
import com.ubs.training.server.dao.model.Type;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/27/12
 * Time: 11:06 AM
 * To change this template use File | Settings | File Templates.
 */
@Repository("hbmTypeDao")
public class HbmTypeDao implements TypeDao{

    private SessionFactory factory;

    private HibernateTemplate tpl;

    @Autowired
    public void setFactory(SessionFactory factory) {
        tpl = new HibernateTemplate(factory);
        this.factory = factory;
    }

    @Override
    public List<Type> getAll() {
        return tpl.loadAll(Type.class);
    }

    @Override
    public Type getById(int id) {
        return tpl.load(Type.class, id);
    }

    @Override
    public boolean add(Type type) {
        tpl.save(type);
        return true;
    }

    @Override
    public boolean update(Type type) {
        tpl.merge(type);
        tpl.update(type);
        return true;
    }

    @Override
    public boolean remove(Type type) {
        tpl.merge(type);
        tpl.delete(type);
        return true;
    }
}
